package com.yunwaikeji.gy_tool_library.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.widget.LinearLayout;

import com.yunwaikeji.gy_tool_library.R;
import com.yunwaikeji.gy_tool_library.model.GYTagFlexboxLayoutModel;
import com.yunwaikeji.gy_tool_library.info.GYBaseInfo;
import com.yunwaikeji.gy_tool_library.info.GYFlexboxLayoutInfo;
import com.yunwaikeji.gy_tool_library.interface1.OnItemClickListener;
import com.yunwaikeji.gy_tool_library.interface1.OnTagItemClickListener;
import com.yunwaikeji.gy_tool_library.utils.display.GYDisplayUtils;
import com.yunwaikeji.gy_tool_library.view.GYImageView;
import com.yunwaikeji.gy_tool_library.view.GYLinearLayout;
import com.yunwaikeji.gy_tool_library.view.GYTextView;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class GYTagFlexboxLayout extends GYLinearLayout implements GYFlexboxLayoutInfo {
    public GYTagFlexboxLayout(Context context) {
        this(context, null);
    }

    public GYTagFlexboxLayout(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public GYTagFlexboxLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(attrs);
    }

    private int clickType = Click.NORMAL;

    private int itemTextSizePx = GYDisplayUtils.sp2px(getContext(), 14);
    private int itemTextColor = Color.BLACK;
    private int itemEdgePx = 0;
    private int itemEdgeColor = Color.TRANSPARENT;
    private int itemBackgroundColor = Color.WHITE;
    private int itemPressColor = GYBaseInfo.WRAP_CONTENT;
    private int itemRadiusPx = 0;

    private int checkedTextSizePx = GYBaseInfo.WRAP_CONTENT;
    private int checkedTextColor = GYBaseInfo.WRAP_CONTENT;
    private int checkedEdgePx = GYBaseInfo.WRAP_CONTENT;
    private int checkedEdgeColor = GYBaseInfo.WRAP_CONTENT;
    private int checkedBackgroundColor = GYBaseInfo.WRAP_CONTENT;
    private int checkedRadiusPx = GYBaseInfo.WRAP_CONTENT;

    private int noCheckedTextSizePx = GYBaseInfo.WRAP_CONTENT;
    private int noCheckedTextColor = GYBaseInfo.WRAP_CONTENT;
    private int noCheckedEdgePx = GYBaseInfo.WRAP_CONTENT;
    private int noCheckedEdgeColor = GYBaseInfo.WRAP_CONTENT;
    private int noCheckedBackgroundColor = Color.TRANSPARENT;
    private int noCheckedRadiusPx = GYBaseInfo.WRAP_CONTENT;

    private int itemPaddingPx = GYBaseInfo.WRAP_CONTENT;
    private int itemPaddingLeftPx = 0;
    private int itemPaddingTopPx = 0;
    private int itemPaddingRightPx = 0;
    private int itemPaddingBottomPx = 0;
    private int itemWidthPx = GYBaseInfo.WRAP_CONTENT;
    private int itemHeightPx = GYBaseInfo.WRAP_CONTENT;
    private String checkedPositions;
    private String noCheckedPositions;

    private boolean isMultiline = true;//是否可以多行
    private boolean isWidthFull = false;//item宽度是否充满
    private int maxLineCount = 0;//每行最多item的数量，大于0开始生效

    private int spacingHorizontalPx = 0;
    private int spacingVerticalPx = 0;

    private OnTagItemClickListener onTagItemClickListener;
    private GYFlexboxLayout gyFlexboxLayout;
    private List<GYTagFlexboxLayoutModel<GYTextView>> allBeanList = new ArrayList<>();
    private GYImageView moreClick;
    private int moreClickRotation = 0;
    private boolean isDown = true;
    private boolean cancelLast = false;//是否可以取消最后一个

    private void init(AttributeSet attributeSet) {
        setOrientation(LinearLayout.HORIZONTAL);
        gyFlexboxLayout = new GYFlexboxLayout(getContext());
        moreClick = new GYImageView(getContext());
        getViewSet().addViews(gyFlexboxLayout, moreClick);
        gyFlexboxLayout.getViewSet().setSizePx(0, WRAP_CONTENT, 1);
        Drawable moreClickDrawable;
        if (attributeSet != null) {
            TypedArray typedArray = getContext().obtainStyledAttributes(attributeSet, R.styleable.GYTagFlexboxLayout);
            clickType = typedArray.getInteger(R.styleable.GYTagFlexboxLayout_clickType, clickType);
            itemTextSizePx = typedArray.getDimensionPixelSize(R.styleable.GYTagFlexboxLayout_itemTextSize, itemTextSizePx);
            itemTextColor = typedArray.getColor(R.styleable.GYTagFlexboxLayout_itemTextColor, itemTextColor);
            itemEdgePx = typedArray.getDimensionPixelSize(R.styleable.GYTagFlexboxLayout_itemEdgeWidth, itemEdgePx);
            itemEdgeColor = typedArray.getColor(R.styleable.GYTagFlexboxLayout_itemEdgeColor, itemEdgeColor);
            itemBackgroundColor = typedArray.getColor(R.styleable.GYTagFlexboxLayout_itemBackgroundColor, itemBackgroundColor);
            itemPressColor = typedArray.getColor(R.styleable.GYTagFlexboxLayout_itemPressColor, itemPressColor);
            itemRadiusPx = typedArray.getDimensionPixelSize(R.styleable.GYTagFlexboxLayout_itemRadius, itemRadiusPx);
            checkedTextSizePx = typedArray.getDimensionPixelSize(R.styleable.GYTagFlexboxLayout_checkedTextSize, checkedTextSizePx);
            checkedTextColor = typedArray.getColor(R.styleable.GYTagFlexboxLayout_checkedTextColor, checkedTextColor);
            checkedEdgePx = typedArray.getDimensionPixelSize(R.styleable.GYTagFlexboxLayout_checkedEdgeWidth, checkedEdgePx);
            checkedEdgeColor = typedArray.getColor(R.styleable.GYTagFlexboxLayout_checkedEdgeColor, checkedEdgeColor);
            checkedBackgroundColor = typedArray.getColor(R.styleable.GYTagFlexboxLayout_checkedBackgroundColor, checkedBackgroundColor);
            checkedRadiusPx = typedArray.getDimensionPixelSize(R.styleable.GYTagFlexboxLayout_checkedRadius, checkedRadiusPx);
            noCheckedTextSizePx = typedArray.getDimensionPixelSize(R.styleable.GYTagFlexboxLayout_noCheckedTextSize, checkedTextSizePx);
            noCheckedTextColor = typedArray.getColor(R.styleable.GYTagFlexboxLayout_noCheckedTextColor, noCheckedTextColor);
            noCheckedEdgePx = typedArray.getDimensionPixelSize(R.styleable.GYTagFlexboxLayout_noCheckedEdgeWidth, noCheckedEdgePx);
            noCheckedEdgeColor = typedArray.getColor(R.styleable.GYTagFlexboxLayout_noCheckedEdgeColor, noCheckedEdgeColor);
            noCheckedBackgroundColor = typedArray.getColor(R.styleable.GYTagFlexboxLayout_noCheckedBackgroundColor, noCheckedBackgroundColor);
            noCheckedRadiusPx = typedArray.getDimensionPixelSize(R.styleable.GYTagFlexboxLayout_noCheckedRadius, noCheckedRadiusPx);
            itemPaddingPx = typedArray.getDimensionPixelSize(R.styleable.GYTagFlexboxLayout_itemPadding, itemPaddingPx);
            itemPaddingLeftPx = typedArray.getDimensionPixelSize(R.styleable.GYTagFlexboxLayout_itemPaddingLeft, itemPaddingLeftPx);
            itemPaddingTopPx = typedArray.getDimensionPixelSize(R.styleable.GYTagFlexboxLayout_itemPaddingTop, itemPaddingTopPx);
            itemPaddingRightPx = typedArray.getDimensionPixelSize(R.styleable.GYTagFlexboxLayout_itemPaddingRight, itemPaddingRightPx);
            itemPaddingBottomPx = typedArray.getDimensionPixelSize(R.styleable.GYTagFlexboxLayout_itemPaddingBottom, itemPaddingBottomPx);
            itemWidthPx = typedArray.getDimensionPixelSize(R.styleable.GYTagFlexboxLayout_itemWidth, itemWidthPx);
            itemHeightPx = typedArray.getDimensionPixelSize(R.styleable.GYTagFlexboxLayout_itemHeight, itemHeightPx);
            checkedPositions = typedArray.getString(R.styleable.GYTagFlexboxLayout_checkedPositions);
            noCheckedPositions = typedArray.getString(R.styleable.GYTagFlexboxLayout_noCheckedPositions);
            spacingHorizontalPx = typedArray.getDimensionPixelSize(R.styleable.GYTagFlexboxLayout_spacingHorizontal, spacingHorizontalPx);
            spacingVerticalPx = typedArray.getDimensionPixelSize(R.styleable.GYTagFlexboxLayout_spacingVertical, spacingVerticalPx);
            isMultiline = typedArray.getBoolean(R.styleable.GYTagFlexboxLayout_isMultiline, isMultiline);
            isWidthFull = typedArray.getBoolean(R.styleable.GYTagFlexboxLayout_isWidthFull, isWidthFull);
            cancelLast = typedArray.getBoolean(R.styleable.GYTagFlexboxLayout_cancelLast, cancelLast);
            moreClickRotation = typedArray.getInteger(R.styleable.GYTagFlexboxLayout_moreClickRotation, moreClickRotation);
            moreClickDrawable = typedArray.getDrawable(R.styleable.GYTagFlexboxLayout_moreClickDrawable);
            if (moreClickDrawable == null) {
                moreClickDrawable = getResources().getDrawable(R.drawable.gylibrary_left_black);
                moreClickRotation -= 90;
            }
            moreClick.setRotation(moreClickRotation);
            moreClick.getViewSet()
                    .setSizePx(typedArray.getDimensionPixelSize(R.styleable.GYTagFlexboxLayout_moreClickDrawableWidth, itemTextSizePx), typedArray.getDimensionPixelSize(R.styleable.GYTagFlexboxLayout_moreClickDrawableHeight, itemTextSizePx))
                    .setMarginPx(typedArray.getDimensionPixelSize(R.styleable.GYTagFlexboxLayout_moreClickMarginLeft, 0), 0, 0, 0)
                    .toView()
                    .getImageBuilder()
                    .setImageDrawable(moreClickDrawable)
                    .build();
            setShowMoreClick(typedArray.getBoolean(R.styleable.GYTagFlexboxLayout_showMoreClick, false));
            typedArray.recycle();
        }
        if (itemPressColor == GYBaseInfo.WRAP_CONTENT) {
            itemPressColor = itemBackgroundColor;
        }
        if (checkedTextSizePx == GYBaseInfo.WRAP_CONTENT) {
            checkedTextSizePx = itemTextSizePx;
        }
        if (noCheckedTextSizePx == GYBaseInfo.WRAP_CONTENT) {
            noCheckedTextSizePx = itemTextSizePx;
        }
        if (checkedEdgePx == GYBaseInfo.WRAP_CONTENT) {
            checkedEdgePx = itemEdgePx;
        }
        if (noCheckedEdgePx == GYBaseInfo.WRAP_CONTENT) {
            noCheckedEdgePx = itemEdgePx;
        }
        if (checkedEdgeColor == GYBaseInfo.WRAP_CONTENT) {
            checkedEdgeColor = itemEdgeColor;
        }
        if (noCheckedEdgeColor == GYBaseInfo.WRAP_CONTENT) {
            noCheckedEdgeColor = itemEdgeColor;
        }
        if (itemPaddingPx != GYBaseInfo.WRAP_CONTENT) {
            itemPaddingLeftPx = itemPaddingPx;
            itemPaddingTopPx = itemPaddingPx;
            itemPaddingRightPx = itemPaddingPx;
            itemPaddingBottomPx = itemPaddingPx;
        }
        if (checkedRadiusPx == GYBaseInfo.WRAP_CONTENT) {
            checkedRadiusPx = itemRadiusPx;
        }
        if (noCheckedRadiusPx == GYBaseInfo.WRAP_CONTENT) {
            noCheckedRadiusPx = itemRadiusPx;
        }
        if (noCheckedTextColor == GYBaseInfo.WRAP_CONTENT) {
            noCheckedTextColor = itemTextColor;
        }
        if (checkedBackgroundColor == GYBaseInfo.WRAP_CONTENT) {
            checkedBackgroundColor = itemPressColor;
        }
        if (checkedTextColor == GYBaseInfo.WRAP_CONTENT) {
            checkedTextColor = itemPressColor;
        }
        gyFlexboxLayout.getViewSet().setMultiline(true);
        gyFlexboxLayout.getViewSet().setSpacingHorizontalPx(spacingHorizontalPx);
        gyFlexboxLayout.getViewSet().setSpacingVerticalPx(spacingVerticalPx);
        gyFlexboxLayout.getViewSet().setMultiline(isMultiline);
        gyFlexboxLayout.getViewSet().setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(View clickView, int clickPosition) {
                boolean checked = allBeanList.get(clickPosition).isChecked();
                if (!allBeanList.get(clickPosition).isNoChecked()) {
                    switch (clickType) {
                        case Click.NORMAL:
                            checked = false;
                            break;
                        case Click.CHECK_ONE:
                            checked = !checked;
                            setCheckedAll(false);
                            setChecked(checked, clickPosition);
                            break;
                        case Click.CHECK_MANY:
                            checked = !checked;
                            setChecked(checked, clickPosition);
                            break;
                    }
                }
                if (onTagItemClickListener != null) {
                    onTagItemClickListener.onTagItemClick(clickView, clickPosition, checked);
                }
            }
        });
        List<View> allViewList = new ArrayList<>();
        moreClick.setOnClickListener(v -> {
            isDown = !isDown;
            if (isDown) {
                moreClick.setRotation(moreClickRotation);
                gyFlexboxLayout.getViewSet().setViews(allViewList);
            } else {
                moreClick.setRotation(moreClickRotation + 180);
                allViewList.clear();
                allViewList.addAll(gyFlexboxLayout.getViewSet().getChildViews());
                gyFlexboxLayout.getViewSet().setViews(gyFlexboxLayout.getViewSet().getViewListList().get(0));
            }
        });
        refreshCheckedPositions();
        refreshNoCheckedPositions();
    }

    public GYTagFlexboxLayout setCancelLast(boolean cancelLast) {
        this.cancelLast = cancelLast;
        return this;
    }

    private void refreshCheckedPositions() {
        if (!TextUtils.isEmpty(checkedPositions)) {
            String[] strings = checkedPositions.split(",");
            List<Integer> integerList = new ArrayList<>();
            for (String string : strings) {
                integerList.add(Integer.parseInt(string));
            }
            setChecked(true, integerList);
        }
    }

    public GYTagFlexboxLayout refresh() {
        for (GYTagFlexboxLayoutModel<GYTextView> gyTagFlexboxLayoutModel : allBeanList) {
            initTextView(gyTagFlexboxLayoutModel);
        }
        return this;
    }

    private void refreshNoCheckedPositions() {
        if (!TextUtils.isEmpty(noCheckedPositions)) {
            String[] strings = noCheckedPositions.split(",");
            List<Integer> integerList = new ArrayList<>();
            for (String string : strings) {
                integerList.add(Integer.parseInt(string));
            }
            setNoChecked(true, integerList);
        }
    }

    public GYTagFlexboxLayout setCheckedAll(boolean checked) {
        for (GYTagFlexboxLayoutModel<GYTextView> gyTagFlexboxLayoutModel : allBeanList) {
            gyTagFlexboxLayoutModel.setChecked(checked);
            initTextView(gyTagFlexboxLayoutModel);
        }
        return this;
    }

    public GYTagFlexboxLayout setChecked(boolean checked, Integer... positions) {
        return setChecked(checked, Arrays.asList(positions));
    }

    public GYTagFlexboxLayout setChecked(boolean checked, List<Integer> positionList) {
        for (int i = 0; i < allBeanList.size(); i++) {
            if (positionList.contains(i)) {
                if (getCheckedList().size() <= 1) {
                    checked = true;
                }
                allBeanList.get(i).setChecked(checked);
                initTextView(allBeanList.get(i));
            }
        }
        return this;
    }

    public GYTagFlexboxLayout setNoCheckedAll(boolean noChecked) {
        for (GYTagFlexboxLayoutModel<GYTextView> gyTagFlexboxLayoutModel : allBeanList) {
            gyTagFlexboxLayoutModel.setNoChecked(noChecked);
            initTextView(gyTagFlexboxLayoutModel);
        }
        return this;
    }

    public GYTagFlexboxLayout setNoChecked(boolean noChecked, Integer... positions) {
        return setNoChecked(noChecked, Arrays.asList(positions));
    }

    public GYTagFlexboxLayout setNoChecked(boolean noChecked, List<Integer> positionList) {
        for (int i = 0; i < allBeanList.size(); i++) {
            if (positionList.contains(i)) {
                allBeanList.get(i).setNoChecked(noChecked);
                initTextView(allBeanList.get(i));
            }
        }
        return this;
    }

    /**
     * 添加String
     *
     * @param strings
     * @return
     */
    public GYTagFlexboxLayout addStrings(String... strings) {
        return addStrings(Arrays.asList(strings));
    }

    public GYTagFlexboxLayout addStrings(List<String> stringList) {
        return addStrings(-1, stringList);
    }

    public GYTagFlexboxLayout addStrings(int index, String... strings) {
        return addStrings(index, Arrays.asList(strings));
    }

    public GYTagFlexboxLayout addStrings(int index, List<String> stringList) {
        for (String string : stringList) {
            GYTagFlexboxLayoutModel<GYTextView> gyTagFlexboxLayoutModel = new GYTagFlexboxLayoutModel<>();
            GYTextView gyTextView = new GYTextView(getContext()).getViewSet().setText(string).toView();
            GYTagFlexboxLayoutModel<GYTextView> gyTextViewGYTagFlexboxLayoutModel = new GYTagFlexboxLayoutModel<>();
            gyTextViewGYTagFlexboxLayoutModel.setView(gyTextView);
            gyTagFlexboxLayoutModel.setView(initTextView(gyTextViewGYTagFlexboxLayoutModel));
            gyTagFlexboxLayoutModel.setChecked(false);
            gyTagFlexboxLayoutModel.setNoChecked(false);
            gyTagFlexboxLayoutModel.setName(string);
            if (index < 0) {
                allBeanList.add(gyTagFlexboxLayoutModel);
                gyFlexboxLayout.getViewSet().addViews(gyTagFlexboxLayoutModel.getView());
            } else {
                allBeanList.add(index, gyTagFlexboxLayoutModel);
                gyFlexboxLayout.getViewSet().addViews(index, gyTagFlexboxLayoutModel.getView());
                index++;
            }
        }
        for (int i = 0; i < allBeanList.size(); i++) {
            allBeanList.get(i).setId(i);
        }

        refreshCheckedPositions();
        refreshNoCheckedPositions();
        gyFlexboxLayout.refreshView();
        return this;
    }

    public GYTagFlexboxLayout setStrings(String... strings) {
        return setStrings(Arrays.asList(strings));
    }

    public GYTagFlexboxLayout setStrings(List<String> stringList) {
        removeAllViews();
        return addStrings(stringList);
    }

    public GYTextView initTextView(GYTagFlexboxLayoutModel<GYTextView> gyTagFlexboxLayoutModel) {
        GYTextView gyTextView = gyTagFlexboxLayoutModel.getView();
        if (gyTagFlexboxLayoutModel.isNoChecked()) {
            gyTextView.getBackgroundBuilder().setBackgroundColor(noCheckedBackgroundColor)
                    .setEdgeWidthPx(noCheckedEdgePx)
                    .setEdgeColor(noCheckedEdgeColor)
                    .setRadiusPx(noCheckedRadiusPx)
                    .build()
                    .getViewSet()
                    .setTextSizePx(noCheckedTextSizePx)
                    .setTextColor(noCheckedTextColor)
                    .setClickable(false);
        } else {
            if (gyTagFlexboxLayoutModel.isChecked()) {
                gyTextView.getBackgroundBuilder().setBackgroundColor(checkedBackgroundColor)
                        .setChangeColor(checkedBackgroundColor)
                        .setEdgeWidthPx(checkedEdgePx)
                        .setEdgeColor(checkedEdgeColor)
                        .setRadiusPx(checkedRadiusPx)
                        .build()
                        .getViewSet()
                        .setTextSizePx(checkedTextSizePx)
                        .setTextColor(checkedTextColor)
                        .setClickable(true);
            } else {
                gyTextView.getBackgroundBuilder().setBackgroundColor(itemBackgroundColor)
                        .setChangeColor(itemPressColor)
                        .setEdgeWidthPx(itemEdgePx)
                        .setEdgeColor(itemEdgeColor)
                        .setRadiusPx(itemRadiusPx)
                        .build()
                        .getViewSet()
                        .setTextSizePx(itemTextSizePx)
                        .setTextColor(itemTextColor)
                        .setClickable(true);
            }
        }
        gyTextView.getViewSet().setGravity(Gravity.CENTER)
                .setPaddingPx(itemPaddingLeftPx, itemPaddingTopPx, itemPaddingRightPx, itemPaddingBottomPx);

        if (itemHeightPx != GYBaseInfo.WRAP_CONTENT) {
            gyTextView.getViewSet().setHeightPx(itemHeightPx);
        }
        if (isWidthFull) {
//            gyTextView.setWeight(1);
            gyTextView.getViewSet().setWidthPx(MATCH_PARENT);
        } else {
            gyTextView.getViewSet().setWidthPx(itemWidthPx);
        }
        return gyTextView;
    }

    public GYTagFlexboxLayout setWidthFull(boolean widthFull) {
        isWidthFull = widthFull;
        refresh();
        return this;
    }

    public GYTagFlexboxLayout setMultiline(boolean isMultiline) {
        this.isMultiline = isMultiline;
        gyFlexboxLayout.getViewSet().setMultiline(isMultiline);
        return this;
    }

    public boolean isMultiline() {
        return isMultiline;
    }

    /**
     * 获取横向间距
     *
     * @return
     */
    public float getSpacingHorizontalPx() {
        return gyFlexboxLayout.getViewSet().getSpacingHorizontalPx();
    }

    /**
     * 设置横向间距
     *
     * @return
     */
    public GYTagFlexboxLayout setSpacingHorizontalDp(int spacingHorizontalDp) {
        return setSpacingHorizontalPx(GYDisplayUtils.dp2px(getContext(), spacingHorizontalDp));
    }

    public GYTagFlexboxLayout setSpacingHorizontalPx(int spacingHorizontalPx) {
        gyFlexboxLayout.getViewSet().setSpacingHorizontalPx(spacingHorizontalPx);
        gyFlexboxLayout.refreshView();
        return this;
    }

    /**
     * 获取纵向间距
     *
     * @return
     */
    public float getSpacingVerticalPx() {
        return gyFlexboxLayout.getViewSet().getSpacingVerticalPx();
    }

    /**
     * 设置纵向间距
     *
     * @param spacingVerticalDp
     * @return
     */
    public GYTagFlexboxLayout setSpacingVerticalDp(int spacingVerticalDp) {
        return setSpacingVerticalPx(GYDisplayUtils.dp2px(getContext(), spacingVerticalDp));
    }

    /**
     * 设置纵向间距
     *
     * @return
     */
    public GYTagFlexboxLayout setSpacingVerticalPx(int spacingVerticalPx) {
        gyFlexboxLayout.getViewSet().setSpacingVerticalPx(spacingVerticalPx);
        gyFlexboxLayout.refreshView();
        return this;
    }

    public int getClickType() {
        return clickType;
    }

    public GYTagFlexboxLayout setClickType(int clickType) {
        this.clickType = clickType;
        return this;
    }

    public int getItemTextSizePx() {
        return itemTextSizePx;
    }

    public GYTagFlexboxLayout setItemTextSizePx(int itemTextSizePx) {
        this.itemTextSizePx = itemTextSizePx;
        refresh();
        return this;
    }

    public GYTagFlexboxLayout setItemTextSizeSp(int itemTextSizeSp) {
        return setItemTextSizePx(GYDisplayUtils.sp2px(getContext(), itemTextSizeSp));
    }

    public int getItemTextColor() {
        return itemTextColor;
    }

    public GYTagFlexboxLayout setItemTextColor(int itemTextColor) {
        this.itemTextColor = itemTextColor;
        refresh();
        return this;
    }

    public int getItemEdgePx() {
        return itemEdgePx;
    }

    public GYTagFlexboxLayout setItemEdgePx(int itemEdgePx) {
        this.itemEdgePx = itemEdgePx;
        refresh();
        return this;
    }

    public GYTagFlexboxLayout setItemEdgeDp(int itemEdgeDp) {
        return setItemEdgePx(GYDisplayUtils.dp2px(getContext(), itemEdgeDp));
    }

    public int getItemEdgeColor() {
        return itemEdgeColor;
    }

    public GYTagFlexboxLayout setItemEdgeColor(int itemEdgeColor) {
        this.itemEdgeColor = itemEdgeColor;
        refresh();
        return this;
    }

    public int getItemBackgroundColor() {
        return itemBackgroundColor;
    }

    public GYTagFlexboxLayout setItemBackgroundColor(int itemBackgroundColor) {
        this.itemBackgroundColor = itemBackgroundColor;
        refresh();
        return this;
    }

    public int getItemPressColor() {
        return itemPressColor;
    }

    public GYTagFlexboxLayout setItemPressColor(int itemPressColor) {
        this.itemPressColor = itemPressColor;
        refresh();
        return this;
    }

    public int getItemRadiusPx() {
        return itemRadiusPx;
    }

    public GYTagFlexboxLayout setItemRadiusPx(int itemRadiusPx) {
        this.itemRadiusPx = itemRadiusPx;
        refresh();
        return this;
    }

    public GYTagFlexboxLayout setItemRadiusDp(int itemRadiusDp) {
        return setItemRadiusPx(GYDisplayUtils.dp2px(getContext(), itemRadiusDp));
    }

    public int getCheckedTextSizePx() {
        return checkedTextSizePx;
    }

    public GYTagFlexboxLayout setCheckedTextSizePx(int checkedTextSizePx) {
        this.checkedTextSizePx = checkedTextSizePx;
        refresh();
        return this;
    }

    public GYTagFlexboxLayout setCheckedTextSizeSp(int checkedTextSizeSp) {
        return setCheckedTextSizePx(GYDisplayUtils.sp2px(getContext(), checkedTextSizeSp));
    }

    public int getCheckedTextColor() {
        return checkedTextColor;
    }

    public GYTagFlexboxLayout setCheckedTextColor(int checkedTextColor) {
        this.checkedTextColor = checkedTextColor;
        refresh();
        return this;
    }

    public int getCheckedEdgePx() {
        return checkedEdgePx;
    }

    public GYTagFlexboxLayout setCheckedEdgePx(int checkedEdgePx) {
        this.checkedEdgePx = checkedEdgePx;
        refresh();
        return this;
    }

    public GYTagFlexboxLayout setCheckedEdgeDp(int checkedEdgeDp) {
        return setCheckedEdgePx(GYDisplayUtils.dp2px(getContext(), checkedEdgeDp));
    }

    public int getCheckedEdgeColor() {
        return checkedEdgeColor;
    }

    public GYTagFlexboxLayout setCheckedEdgeColor(int checkedEdgeColor) {
        this.checkedEdgeColor = checkedEdgeColor;
        refresh();
        return this;
    }

    public int getCheckedBackgroundColor() {
        return checkedBackgroundColor;
    }

    public GYTagFlexboxLayout setCheckedBackgroundColor(int checkedBackgroundColor) {
        this.checkedBackgroundColor = checkedBackgroundColor;
        refresh();
        return this;
    }

    public int getCheckedRadiusPx() {
        return checkedRadiusPx;
    }

    public GYTagFlexboxLayout setCheckedRadiusPx(int checkedRadiusPx) {
        this.checkedRadiusPx = checkedRadiusPx;
        refresh();
        return this;
    }

    public GYTagFlexboxLayout setCheckedRadiusDp(int checkedRadiusDp) {
        return setCheckedRadiusPx(GYDisplayUtils.dp2px(getContext(), checkedRadiusDp));
    }

    public int getNoCheckedTextSizePx() {
        return noCheckedTextSizePx;
    }

    public GYTagFlexboxLayout setNoCheckedTextSizePx(int noCheckedTextSizePx) {
        this.noCheckedTextSizePx = noCheckedTextSizePx;
        refresh();
        return this;
    }

    public GYTagFlexboxLayout setNoCheckedTextSizeSp(int noCheckedTextSizeSp) {
        return setNoCheckedTextSizePx(GYDisplayUtils.sp2px(getContext(), noCheckedTextSizeSp));
    }

    public int getNoCheckedTextColor() {
        return noCheckedTextColor;
    }

    public GYTagFlexboxLayout setNoCheckedTextColor(int noCheckedTextColor) {
        this.noCheckedTextColor = noCheckedTextColor;
        refresh();
        return this;
    }

    public int getNoCheckedEdgePx() {
        return noCheckedEdgePx;
    }

    public GYTagFlexboxLayout setNoCheckedEdgePx(int noCheckedEdgePx) {
        this.noCheckedEdgePx = noCheckedEdgePx;
        refresh();
        return this;
    }

    public GYTagFlexboxLayout setNoCheckedEdgeDp(int noCheckedEdgeDp) {
        return setNoCheckedEdgePx(GYDisplayUtils.dp2px(getContext(), noCheckedEdgeDp));
    }

    public int getNoCheckedEdgeColor() {
        return noCheckedEdgeColor;
    }

    public GYTagFlexboxLayout setNoCheckedEdgeColor(int noCheckedEdgeColor) {
        this.noCheckedEdgeColor = noCheckedEdgeColor;
        refresh();
        return this;
    }

    public int getNoCheckedBackgroundColor() {
        return noCheckedBackgroundColor;
    }

    public GYTagFlexboxLayout setNoCheckedBackgroundColor(int noCheckedBackgroundColor) {
        this.noCheckedBackgroundColor = noCheckedBackgroundColor;
        refresh();
        return this;
    }

    public int getNoCheckedRadiusPx() {
        return noCheckedRadiusPx;
    }

    public GYTagFlexboxLayout setNoCheckedRadiusPx(int noCheckedRadiusPx) {
        this.noCheckedRadiusPx = noCheckedRadiusPx;
        refresh();
        return this;
    }

    public GYTagFlexboxLayout setNoCheckedRadiusDp(int noCheckedRadiusDp) {
        return setNoCheckedRadiusPx(GYDisplayUtils.dp2px(getContext(), noCheckedRadiusDp));
    }

    public int getItemPaddingPx() {
        return itemPaddingPx;
    }

    public GYTagFlexboxLayout setItemPaddingPx(int itemPaddingPx) {
        this.itemPaddingPx = itemPaddingPx;
        this.itemPaddingLeftPx = itemPaddingPx;
        this.itemPaddingTopPx = itemPaddingPx;
        this.itemPaddingRightPx = itemPaddingPx;
        this.itemPaddingBottomPx = itemPaddingPx;
        refresh();
        return this;
    }

    public GYTagFlexboxLayout setItemPaddingDp(int itemPaddingDp) {
        return setItemPaddingPx(GYDisplayUtils.dp2px(getContext(), itemPaddingDp));
    }

    public int getItemPaddingLeftPx() {
        return itemPaddingLeftPx;
    }

    public GYTagFlexboxLayout setItemPaddingLeftPx(int itemPaddingLeftPx) {
        this.itemPaddingLeftPx = itemPaddingLeftPx;
        refresh();
        return this;
    }

    public GYTagFlexboxLayout setItemPaddingLeftDp(int itemPaddingLeftDp) {
        return setItemPaddingLeftPx(GYDisplayUtils.dp2px(getContext(), itemPaddingLeftDp));
    }

    public int getItemPaddingTopPx() {
        return itemPaddingTopPx;
    }

    public GYTagFlexboxLayout setItemPaddingTopPx(int itemPaddingTopPx) {
        this.itemPaddingTopPx = itemPaddingTopPx;
        refresh();
        return this;
    }

    public GYTagFlexboxLayout setItemPaddingTopDp(int itemPaddingTopDp) {
        return setItemPaddingTopPx(GYDisplayUtils.dp2px(getContext(), itemPaddingTopDp));
    }

    public int getItemPaddingRightPx() {
        return itemPaddingRightPx;
    }

    public GYTagFlexboxLayout setItemPaddingRightPx(int itemPaddingRightPx) {
        this.itemPaddingRightPx = itemPaddingRightPx;
        refresh();
        return this;
    }

    public GYTagFlexboxLayout setItemPaddingRightDp(int itemPaddingRightDp) {
        return setItemPaddingRightPx(GYDisplayUtils.dp2px(getContext(), itemPaddingRightDp));
    }

    public int getItemPaddingBottomPx() {
        return itemPaddingBottomPx;
    }

    public GYTagFlexboxLayout setItemPaddingBottomPx(int itemPaddingBottomPx) {
        this.itemPaddingBottomPx = itemPaddingBottomPx;
        refresh();
        return this;
    }

    public GYTagFlexboxLayout setItemPaddingBottomDp(int itemPaddingBottomDp) {
        return setItemPaddingBottomPx(GYDisplayUtils.dp2px(getContext(), itemPaddingBottomDp));
    }

    public int getItemWidthPx() {
        return itemWidthPx;
    }

    public GYTagFlexboxLayout setItemWidthPx(int itemWidthPx) {
        this.itemWidthPx = itemWidthPx;
        refresh();
        return this;
    }

    public GYTagFlexboxLayout setItemWidthDp(int itemWidthDp) {
        return setItemWidthPx(GYDisplayUtils.dp2px(getContext(), itemWidthDp));
    }

    public int getItemHeightPx() {
        return itemHeightPx;
    }

    public GYTagFlexboxLayout setItemHeightPx(int itemHeightPx) {
        this.itemHeightPx = itemHeightPx;
        refresh();
        return this;
    }

    public GYTagFlexboxLayout setItemHeightDp(int itemHeightDp) {
        return setItemHeightPx(GYDisplayUtils.dp2px(getContext(), itemHeightDp));
    }

    public String getCheckedPositions() {
        return checkedPositions;
    }

    public GYTagFlexboxLayout setCheckedPositions(String checkedPositions) {
        this.checkedPositions = checkedPositions;
        refresh();
        return this;
    }

    public String getNoCheckedPositions() {
        return noCheckedPositions;
    }

    public GYTagFlexboxLayout setNoCheckedPositions(String noCheckedPositions) {
        this.noCheckedPositions = noCheckedPositions;
        refresh();
        return this;
    }

    public GYTagFlexboxLayout setOnTagItemClickListener(OnTagItemClickListener onTagItemClickListener) {
        this.onTagItemClickListener = onTagItemClickListener;
        refresh();
        return this;
    }

    public List<GYTagFlexboxLayoutModel<GYTextView>> getAllList() {
        return allBeanList;
    }

    public List<GYTagFlexboxLayoutModel<GYTextView>> getCheckedList() {
        List<GYTagFlexboxLayoutModel<GYTextView>> list = new ArrayList<>();
        for (GYTagFlexboxLayoutModel<GYTextView> bean : allBeanList) {
            if (!bean.isNoChecked() && bean.isChecked()) {
                list.add(bean);
            }
        }
        return list;
    }

    public List<GYTagFlexboxLayoutModel<GYTextView>> getItemList() {
        List<GYTagFlexboxLayoutModel<GYTextView>> list = new ArrayList<>();
        for (GYTagFlexboxLayoutModel<GYTextView> bean : allBeanList) {
            if (!bean.isNoChecked() && !bean.isChecked()) {
                list.add(bean);
            }
        }
        return list;
    }

    public List<GYTagFlexboxLayoutModel<GYTextView>> getNoCheckedList() {
        List<GYTagFlexboxLayoutModel<GYTextView>> list = new ArrayList<>();
        for (GYTagFlexboxLayoutModel<GYTextView> bean : allBeanList) {
            if (bean.isNoChecked()) {
                list.add(bean);
            }
        }
        return list;
    }

    @Override
    public void removeAllViews() {
        allBeanList.clear();
        gyFlexboxLayout.removeAllViews();
    }

    public GYTagFlexboxLayout removeAllViews1() {
        removeAllViews();
        return this;
    }

    public GYTagFlexboxLayout setShowMoreClick(boolean showMoreClick) {
        moreClick.setVisibility(showMoreClick ? VISIBLE : GONE);
        return this;
    }

    public GYImageView getMoreClick() {
        return moreClick;
    }

    public GYTagFlexboxLayout setMoreClickMarginLeftPx(int moreClickMarginLeftPx) {
        moreClick.getViewSet().setMarginPx(moreClickMarginLeftPx, 0, 0, 0);
        return this;
    }

    public GYTagFlexboxLayout setMoreClickMarginLeftDp(int moreClickMarginLeftDp) {
        return setMoreClickMarginLeftPx(GYDisplayUtils.dp2px(getContext(), moreClickMarginLeftDp));
    }
}
